package cn.vic.genertor.other;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * Created by vic on 2017/11/16.
 */
public class BeanDomian {

    /**
     * 打印 集合
     */
    private PrintList printList = new PrintList();


    public void jsonToMarkDown(String json) {
        JSONObject object = JSON.parseObject(json);
        Integer i = new Integer(1);
        each(object, i);
    }

    /**
     * 递归 所有
     *
     * @param object
     */
    public void each(Object object, int index) {
        if (object instanceof Map) {
            JSONObject map = (JSONObject) object;
            for (Map.Entry<String, Object> entrie : map.entrySet()) {
                if (isNext(entrie.getValue())) {
                    printList.addPrint(index, entrie.getKey(), entrie.getValue());
                    each(entrie.getValue(), index + 1);
                } else {
                    printList.addPrint(index, entrie.getKey(), entrie.getValue());
                }
            }
        } else if (object instanceof List) {
            JSONArray list = (JSONArray) object;
            if (list != null && list.size() > 0) {
                Object o = list.get(0);
                if (isNext(o)) {
                    each(o, o instanceof List ? index + 1 : index);
                }
            }
        }
    }


    /**
     * 判断类型是否为8大包装类型
     * 包装类型代表没有集合，也就是没有下一个对象
     *
     * @param obj
     * @return
     */
    private boolean isNext(Object obj) {
        if (obj == null) {
            return false;
        } else if (obj instanceof Integer) {
            return false;
        } else if (obj instanceof Double) {
            return false;
        } else if (obj instanceof BigDecimal) {
            return false;
        } else if (obj instanceof String) {
            return false;
        } else if (obj instanceof Boolean) {
            return false;
        } else if (obj instanceof Character) {
            return false;
        } else if (obj instanceof Long) {
            return false;
        } else if (obj instanceof Float) {
            return false;
        }
        return true;
    }

    public PrintList getPrintList() {
        return printList;
    }
}
